package xddq

import (
	"fmt"
	D "websocket_apn/define"
	"websocket_apn/log"
	"websocket_apn/proto"
)

var XianjuLevel = []int64{
	//1		2	3	4	5		6	7	8	9	0
	100, 200, 72950, 74500, 77600, 83800, 96200, 121000, 146000, 172000, //0
	196000, 223000, 255000, 299000, 330000, 358000, 389000, 421000, 451000, 9996000,
	9996000, 9996000, 9996000, 9996000, 9996000, 9996000, 9996000, 9996000, 9996000, 9996000,
}

func GetXianjuLevel(level int) int {
	ret := level
	if level >= 17 && level < 20 {
		ret = 17
	} else if level >= 20 && level < 22 {
		ret = 20
	} else if level >= 22 && level < 24 {
		ret = 22
	} else if level >= 24 && level < 26 {
		ret = 24
	} else if level >= 26 && level < 29 {
		ret = 26
	} else if level >= 29 && level < 32 {
		ret = 29
	} else if level >= 32 && level < 35 {
		ret = 32
	} else if level >= 35 && level < 38 {
		ret = 35
	} else if level >= 38 && level < 41 {
		ret = 38
	} else if level >= 41 && level < 44 {
		ret = 41
	} else if level >= 44 && level < 47 {
		ret = 44
	} else if level >= 47 {
		ret = 47
	}
	return 0x80 + ret
}

func GetXianjuNum(level int) string {
	ret := 160
	if level == 18 {
		ret = 160
	} else if level == 19 {
		ret = 163
	} else if level > 19 {
		ret = 165
	}
	return fmt.Sprintf("%02X", ret)
}

// 地图数据
var XianjuMapData = map[int]D.XianjuGoodsInfo{
	23169: {ID: "81B5", Name: "寒冰蚕", Level: 1, Price: 69, APrice: 138},
	23170: {ID: "82B5", Name: "赤阳玉珠", Level: 2, Price: 72, APrice: 144},
	23171: {ID: "83B5", Name: "灵虚慧果", Level: 3, Price: 76, APrice: 152},
	23172: {ID: "84B5", Name: "妙谛莲台", Level: 4, Price: 79, APrice: 158},
	23173: {ID: "85B5", Name: "九叶雪莲", Level: 5, Price: 83, APrice: 166},
	23174: {ID: "86B5", Name: "龙魄仙木", Level: 6, Price: 86, APrice: 172},
	23175: {ID: "87B5", Name: "龙鳞仙果", Level: 7, Price: 90, APrice: 180},
	23176: {ID: "88B5", Name: "落日心草", Level: 8, Price: 93, APrice: 186},
	23177: {ID: "89B5", Name: "菩提灵果", Level: 9, Price: 97, APrice: 194},
	23178: {ID: "8AB5", Name: "摇光玄莲", Level: 10, Price: 90, APrice: 200},
	23179: {ID: "8BB5", Name: "北冥寒花", Level: 11, Price: 104, APrice: 208},
	23180: {ID: "8CB5", Name: "天仙玉露", Level: 12, Price: 107, APrice: 214},
	23181: {ID: "8DB5", Name: "水云果", Level: 13, Price: 111, APrice: 222},
	23182: {ID: "8EB5", Name: "麒麟涎", Level: 14, Price: 114, APrice: 228},
	23183: {ID: "8FB5", Name: "仙泪绿金", Level: 15, Price: 118, APrice: 236},
	23184: {ID: "90B5", Name: "玄龟甲", Level: 16, Price: 121, APrice: 242},
	23185: {ID: "91B5", Name: "玄雷铁", Level: 18, Price: 125, APrice: 250},
	23186: {ID: "92B5", Name: "血阳铁", Level: 20, Price: 128, APrice: 256},
	23187: {ID: "93B5", Name: "银雪霜木", Level: 22, Price: 131, APrice: 264},
	23188: {ID: "94B5", Name: "银丝晶", Level: 24, Price: 135, APrice: 270},
	23189: {ID: "95B5", Name: "赤墨灵芝", Level: 26, Price: 138, APrice: 278},
	23190: {ID: "96B5", Name: "玄玉茸", Level: 29, Price: 142, APrice: 284},
	23191: {ID: "97B5", Name: "霞天竹", Level: 32, Price: 145, APrice: 292},
	23192: {ID: "98B5", Name: "火蕴石", Level: 35, Price: 149, APrice: 298},
	23193: {ID: "99B5", Name: "白露天霜", Level: 38, Price: 152, APrice: 306},
	23194: {ID: "9AB5", Name: "玄雀羽", Level: 41, Price: 156, APrice: 312},
	23195: {ID: "9BB5", Name: "九转灵参", Level: 44, Price: 159, APrice: 320},
	23196: {ID: "9CB5", Name: "北冥玄晶", Level: 47, Price: 163, APrice: 326},
}

// 妖盟-查看仙居等级的结果
func (xd *XdDriver) CheckXianjuInfoData(allInfo D.XianjuInfo) {
	xd.UserData.Xianju.Level = allInfo.Level
	xd.UserData.Xianju.Prosperity = allInfo.Prosperity
	needLevelUp := false
	if XianjuLevel[allInfo.Level] < allInfo.Prosperity {
		log.Infof("妖盟仙居-升级仙居")
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YARDPB_LEVEL_UP, ""))
		needLevelUp = true
	}
	buildName := ""
	for _, info := range allInfo.Items {
		log.Infof("妖盟仙居-%+v", info)
		switch info.Type {
		case "EB07": //桃树
			buildName = "桃树"
			log.Infof("开始妖盟仙居-收取桃子")
			xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YARDPB_BUILD_GAIN_REWARD, fmt.Sprintf("08 %s 10 %s", info.ID, info.Type)))
			break
		case "EC07": //灵池
			buildName = "灵池"
			//log.Infof("开始妖盟仙居-收取灵池")
			//xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YARDPB_BUILD_GAIN_REWARD, fmt.Sprintf("08 %s 10 %s", info.ID, info.Type)))
			log.Infof("开始妖盟仙居-灵池终止孕育")
			xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YARDPB_BUILD_MAKE, fmt.Sprintf("08 %s 10 %s 28 01", info.ID, info.Type)))
			log.Infof("开始妖盟仙居-灵池开始孕育 num = %s", info.Num) //20 后面为孕育个数
			if info.Num == "00" {
				info.Num = GetXianjuNum(info.Level)
			}
			xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YARDPB_BUILD_MAKE, fmt.Sprintf("08 %s 10 %s 18 %02X B5 18 20 %s 28 00", info.ID, info.Type, GetXianjuLevel(info.Level), info.Num)))
			break
		case "EA07": //丹炉
			buildName = "丹炉"
			log.Infof("开始妖盟仙居-收取丹炉")
			xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YARDPB_BUILD_GAIN_REWARD, fmt.Sprintf("08 %s 10 %s", info.ID, info.Type)))
			num := xd.UserData.Xianju.LingCao / 500
			if num > 255 {
				num = 255
			}
			log.Infof("开始妖盟仙居-开始炼丹%d个，灵草数量%d", num, xd.UserData.Xianju.LingCao)
			xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YARDPB_BUILD_MAKE, fmt.Sprintf("08 %s 10 %s 20 %02X 28 00", info.ID, info.Type, num)))
			break
		case "E907": //灵草
			buildName = "灵草"
			log.Infof("开始妖盟仙居-收取灵草")
			xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YARDPB_BUILD_GAIN_REWARD, fmt.Sprintf("08 %s 10 %s", info.ID, info.Type)))
			break
		}
		if needLevelUp || info.Level < allInfo.Level {
			log.Infof("开始妖盟仙居-升级%s至%d级", buildName, info.Level+1)
			xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YARDPB_BUILD_LEVEL_UP, fmt.Sprintf("08 %s 10 %s", info.Type, info.ID)))
			xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YARDPB_BUILD_REQUEST_UNION_HELP, fmt.Sprintf("08 %s 10 %s", info.ID, info.Type)))
		}
	}
}

// 查看仙居商品的结果
func (xd *XdDriver) CheckXianjuGoodsInfoData(allInfo []D.XianjuGoodsInfo) {
	flag := false
	for _, info := range allInfo {
		if info.Num == 0 {
			continue
		}
		if infoD, ok := xd.UserData.Xianju.MapD[info.IDi]; ok {
			infoD.Num = info.Num
			xd.UserData.Xianju.MapD[info.IDi] = infoD
			flag = true
			log.Infof("仙居-%s 剩余%d个", infoD.Name, infoD.Num)
		}
	}
	if flag {
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YARDPB_MERCHANT_INFO, "08"+xd.UserData.UserID))
	}
}

// 查看仙居协助的结果
func (xd *XdDriver) CheckXianjuAssistanceInfoData(allInfo []D.XianjuAssistanceInfo) {
	for _, info := range allInfo {
		log.Infof("开始妖盟-仙居协助道友【%s】", info.Name)
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YARDPB_BUILD_UNION_HELP, fmt.Sprintf("08 %s 10 %s 18 %s", info.ID, info.Res1, info.Res2)))
	}
}

// 妖盟-查看仙居等级的结果
func (xd *XdDriver) CheckXianjuLevelInfoData(info D.XianjuLevelInfo) {
	log.Infof("妖盟-获取仙居信息成功, 等级%d,繁荣度%d", info.Level, info.Prosperity)
	xd.UserData.Xianju.Level = info.Level
	xd.UserData.Xianju.Prosperity = info.Prosperity
}

// 妖盟-查看仙居售卖的结果
func (xd *XdDriver) CheckXianjuPriceInfoData(allInfo []D.XianjuPriceInfo) {
	for _, sale := range allInfo {
		if infoD, ok := xd.UserData.Xianju.MapD[sale.IDi]; ok {
			if infoD.Num > 0 {
				log.Infof("仙居-开始售卖%d个%s,售卖价格%d ", infoD.Num, infoD.Name, infoD.APrice)
				xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YARDPB_MERCHANT_EXCHANGE, fmt.Sprintf("08 %s 18 10 %s 18 %s", infoD.ID, proto.IntDataToHexStr(infoD.Num), xd.UserData.UserID)))
			}
		}
	}
}
